home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Dialog Boxes / HeadDump / HeadDump.cs next >
Encoding:
Text File  |  2001-01-15  |  2.5 KB  |  84 lines

  1. //---------------------------------------
  2. // HeadDump.cs ⌐ 2001 by Charles Petzold
  3. //---------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Windows.Forms;
  8.  
  9. class HeadDump: Form
  10. {
  11.      const string strProgName  = "Head Dump";
  12.      string       strFileName = "";
  13.  
  14.      public static void Main()
  15.      {
  16.           Application.Run(new HeadDump());
  17.      }
  18.      public HeadDump()
  19.      {
  20.           Text = strProgName;
  21.           Font = new Font(FontFamily.GenericMonospace, 
  22.                           Font.SizeInPoints);
  23.  
  24.           Menu = new MainMenu();
  25.           Menu.MenuItems.Add("&File");
  26.           Menu.MenuItems[0].MenuItems.Add("&Open...", 
  27.                               new EventHandler(MenuFileOpenOnClick));
  28.           Menu.MenuItems.Add("F&ormat");
  29.           Menu.MenuItems[1].MenuItems.Add("&Font...",
  30.                               new EventHandler(MenuFormatFontOnClick));
  31.      }
  32.      void MenuFileOpenOnClick(object obj, EventArgs ea)
  33.      {
  34.           OpenFileDialog dlg = new OpenFileDialog();
  35.  
  36.           if (dlg.ShowDialog() == DialogResult.OK)
  37.           {
  38.                strFileName = dlg.FileName;
  39.                Text = strProgName + " - " + Path.GetFileName(strFileName);
  40.                Invalidate();
  41.           }
  42.      }
  43.      void MenuFormatFontOnClick(object obj, EventArgs ea)
  44.      {
  45.           FontDialog dlg = new FontDialog();
  46.  
  47.           dlg.Font = Font;
  48.           dlg.FixedPitchOnly = true;
  49.  
  50.           if (dlg.ShowDialog() == DialogResult.OK)
  51.           {
  52.                Font = dlg.Font;
  53.                Invalidate();
  54.           }
  55.      }
  56.      protected override void OnPaint(PaintEventArgs pea)
  57.      {
  58.           Graphics   grfx  = pea.Graphics;
  59.           Brush      brush = new SolidBrush(ForeColor);
  60.           FileStream fs;
  61.  
  62.           try
  63.           {
  64.                fs = new FileStream(strFileName, FileMode.Open,
  65.                                    FileAccess.Read, FileShare.Read);
  66.           }
  67.           catch
  68.           {
  69.                return;
  70.           }
  71.  
  72.           for (int iLine = 0; iLine <= ClientSize.Height / Font.Height; 
  73.                               iLine++)
  74.           {
  75.                byte[] abyBuffer = new byte[16];
  76.                int    iCount    = fs.Read(abyBuffer, 0, 16);
  77.                string str       = HexDump.ComposeLine(16 * iLine, 
  78.                                                       abyBuffer, iCount);
  79.  
  80.                grfx.DrawString(str, Font, brush, 0, iLine * Font.Height);
  81.           }
  82.           fs.Close();
  83.      }
  84.  }